package edu.hubu.book.dao;

import edu.hubu.book.domain.BookClass;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("bookClassDao")
public interface IBookClassDao {
    /**
     * 查询所有分类
     * @return
     */
    @Select("SELECT * FROM `firstclass`\n" +
            "LEFT JOIN secondclass\n" +
            "on firstclass.sid = secondclass.firstId;")
    List<BookClass> findAll();


    /**
     * 添加一级分类
     * @param firstClassName
     * @param description
     */
    @Insert("Insert into firstclass (firstClassName,description) values(#{firstClassName}" +
            ",#{description});")
    int addFirstClass(@Param("firstClassName") String firstClassName,@Param("description") String description);

    /**
     * 添加二级分类时查询是否存在一级分类
     * @param firstId
     */
    @Select("SELECT count(*) from firstclass where sid=#{firstId}")
    int findById(@Param("firstId") Integer firstId);

    /**
     * 添加二级分类
     * @param firstId
     * @param secondClassName
     */
    @Insert("Insert into secondclass (firstId,secondClassName) values(#{firstId},#{secondClassName});")
    int addSecondClass(@Param("firstId") int firstId,@Param("secondClassName") String secondClassName);


    /**
     * 修改一级分类
     * @param sid
     * @param firstClassName
     * @param description
     */
    @Update("UPDATE firstclass set firstClassName=#{firstClassName},description=" +
            "#{description} where sid=#{sid};")
    int updateFirstClass(@Param("sid") int sid,@Param("firstClassName") String firstClassName,
                          @Param("description") String description);

    /**
     * 修改二级分类
     * @param ssid
     * @param firstId
     * @param secondClassName
     */
    @Update("UPDATE secondclass set firstId=#{firstId},secondClassName=#{secondClassName} where ssid = #{ssid};")
    int updateSecondClass(@Param("ssid") int ssid, @Param("firstId") int firstId, @Param("secondClassName") String secondClassName);

    /**
     * 删除一级分类
     * @param
     */
    @Delete("DELETE from firstclass where firstClassName=#{firstClassName}")
    int deleteFirstClass(@Param("firstClassName") String firstClassName);

    /**
     * 删除二级分类
     * @param secondClassName
     */
    @Delete("DELETE from secondclass where secondClassName=#{secondClassName}")
    int deleteSecondClass(String secondClassName);
}
